package org.example.struct.array;

/**
 * @author liyishan
 * @date 2024/6/20 10:21
 * @apiNote
 */

public class Test00852PeakIndexInMountainArray {

    public int peakIndexInMountainArray(int[] arr) {
        int res = -1;
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) {
                res = i;
                break;
            }
        }
        return res;
    }

    public int peakIndexInMountainArray1(int[] arr) {
        int n = arr.length;
        int left = 1, right = n - 2, ans = 0;
        while(left <= right){
            int mid = (right + left) / 2;
            if(arr[mid] > arr[mid+1]){
                ans = mid;
                right = mid -1;
            }else{
                left = mid + 1;
            }
        }
        return ans;
    }

}
